package com.egg.ylt.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.egg.ylt.R;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.DateUtils;
import com.egg.ylt.pojo.GetShopServerListEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by sichaoqi on 2018/8/29.
 */

public class ADA_SelectServiceList extends RecyclerView.Adapter<ADA_SelectServiceList.ServeViewHolder> {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<GetShopServerListEntity.ListEntity> mList;
    private OnItemClickListener itemClickListener;

    public ADA_SelectServiceList(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
        mList = new ArrayList<>();
    }

    //添加数据
    public void addList(List<GetShopServerListEntity.ListEntity> list) {
        if (mList != null) {
            mList.clear();
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }

    //得到当前数据集合
    public List<GetShopServerListEntity.ListEntity> getDataList(){
        return mList;
    }

    @Override
    public ServeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ServeViewHolder(mInflater.inflate(R.layout.item_select_serve_recycler, parent, false));
    }

    @Override
    public void onBindViewHolder(ServeViewHolder holder, final int position) {
        GetShopServerListEntity.ListEntity entity = mList.get(position);

        Glide.with(mContext)
                .load(entity.getImageUrl())
                .error(R.mipmap.ic_serve_default)
                .fallback(R.mipmap.ic_serve_default)
                .into(holder.itemIvImg);
        holder.itemSelectServeTvName.setText(entity.getName());
        holder.itemTvTime.setText("时长" + entity.getServiceTime() + "分钟");
        holder.itemTvNewPrice.setText("¥" + DateUtils.getFormattedMoney(entity.getMemberPrice()));
        holder.itemTvOldPrice.setText(DateUtils.getFormattedMoney(entity.getPrice()));

        //设置原价格中间线
        CustomUtils.setMiddleLine(holder.itemTvOldPrice);
        if (entity.isSelected()){
            holder.itemIvSelector.setSelected(true);
        }else{
            holder.itemIvSelector.setSelected(false);
        }

        //设置选中按钮的监听
        holder.itemIvSelector.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setIsSelected(position);
            }
        });

        //item点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setIsSelected(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void setIsSelected(int position){
        for (int i=0;i<mList.size();i++){
            if (i == position){
                mList.get(i).setSelected(true);
            }else{
                mList.get(i).setSelected(false);
            }
        }
        notifyDataSetChanged();
    }

    class ServeViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_iv_img)
        ImageView itemIvImg;
        @BindView(R.id.item_select_serve_tv_name)
        TextView itemSelectServeTvName;
        @BindView(R.id.item_tv_time)
        TextView itemTvTime;
        @BindView(R.id.item_tv_new_price)
        TextView itemTvNewPrice;
        @BindView(R.id.item_select_serve_tv_xie)
        TextView itemSelectServeTvXie;
        @BindView(R.id.item_tv_old_price)
        TextView itemTvOldPrice;
        @BindView(R.id.item_iv_selector)
        ImageView itemIvSelector;
        public ServeViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public interface OnItemClickListener {
        void onClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.itemClickListener = listener;
    }
}
